﻿using System;
using System.ComponentModel;

namespace FluentBinding.WinForms.Domain
{
    public class Customer : INotifyPropertyChanged
    {
        #region Properties

        #region Fields

        private Country _country;
        private bool _isPremier;

        private string _name;
        private CustomerStatus _status;
        private DateTime _createdAt = DateTime.Now;

        #endregion

        #region Properties

        public Country Country
        {
            get { return _country; }
            set
            {
                if (_country != value)
                {
                    _country = value;
                    PropertyChanged(this, new PropertyChangedEventArgs("Country"));
                }
            }
        }

        public string Name
        {
            get { return _name; }
            set
            {
                if (_name != value)
                {
                    _name = value;
                    PropertyChanged(this, new PropertyChangedEventArgs("Name"));
                    PropertyChanged(this, new PropertyChangedEventArgs("FirstName"));
                    PropertyChanged(this, new PropertyChangedEventArgs("SecondName"));
                }
            }
        }

        public string FirstName
        {
            get
            {
                return _name.IsNullOrEmpty() ? string.Empty : _name.Split(' ')[0];
            }
        }

        public string SecondName
        {
            get
            {
                if (_name.IsNullOrEmpty())
                    return string.Empty;

                var names = _name.Split(' ');

                return names.Length > 1 ? names[1] : string.Empty;
            }
        }

        public bool IsPremier
        {
            get { return _isPremier; }
            set
            {
                if (_isPremier != value)
                {
                    _isPremier = value;
                    PropertyChanged(this, new PropertyChangedEventArgs("IsPremier"));
                }
            }
        }

        public CustomerStatus Status
        {
            get { return _status; }
            set
            {
                if (_status != value)
                {
                    _status = value;
                    PropertyChanged(this, new PropertyChangedEventArgs("Status"));
                }
            }
        }

        public DateTime CreatedAt
        {
            get { return _createdAt; }
            set
            {
                if (_createdAt != value)
                {
                    _createdAt = value;
                    PropertyChanged(this, new PropertyChangedEventArgs("CreatedAt"));
                }
            }
        }        

        public DateTime DateOfBirth { get; set; }

        public int Age { get; set; }

        #endregion

        #endregion

        public Customer()
        {
            DateOfBirth = DateTime.Now;
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged = delegate { };

        #endregion

        #region Testing backdoor

        public Delegate[] GetPropertyChangedInvocationList()
        {
            return PropertyChanged.GetInvocationList();
        }

        #endregion
    }
}